/* $Header: glp_ui.c,v 2.2 01/10/02 10:33:14 przybyls Exp $ */

/***********************************************************************/
/*                                                                     */
/*                  Copyright Hewlett-Packard Co. 1991                 */
/*                                                                     */
/***********************************************************************/

/***********************************************************************/
/* MODULE NAME:  glp.c                                                 */
/* ============                                                        */
/*                                                                     */
/* Author:  Lesley Freed                                               */
/*                                                                     */
/* MODULE PURPOSE:  To provide functions for logging and reporting     */
/*                  glp exceptions.                                    */
/*                                                                     */
/*                                                                     */
/***********************************************************************/


/*****************************/
/*   GLOBAL INCLUDE FILES    */
/*****************************/

#include <typedefs.h>
#include <p_wkfile.h>
#include <wkfile.h>
#include <ramps.h>
#include <zone_config.h>
#include <p_status.h>
#include <zone_status.h>
#include <status.h>
#include <error.h>
#include <messages.h>
#include <p_ui.h>
#include <display.h>

#include <exception.h>
#include <glp.h>

/*****************************/
/*   LOCAL INCLUDE FILES     */
/*****************************/


/*****************************/
/*   GLOBAL VARIABLES        */
/*****************************/


/*****************************/
/*   EXTERNAL VARIABLES      */
/*****************************/


/*****************************/
/*   LOCAL DEFINES           */
/*****************************/


/*****************************/
/*   MACROS                  */
/*****************************/

/***********************************************************************/
/* FUNCTION:  GetMaxDeviation                                          */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:  To return the size of the deviation log.                  */
/*                                                                     */
/* RETURNED VALUE:  INT32                                              */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/

INT32 GetMaxDeviation( void )
{
   return( Glp_Status.log_index );
}


/***********************************************************************/
/* FUNCTION:  GetGlpPresMsg                                            */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:  To return the correct message no. for a given pressure    */
/*           related run deviation.                                    */
/*                                                                     */
/* RETURNED VALUE: U_INT16 msg_no                                      */
/*                                                                     */
/* GLOBALS USED:  none                                                 */
/*                                                                     */
/* CALLS:  none                                                        */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/

/* Look up table to find the correct glp message for a given pressure */
/* deviation.                                                         */
/* NOTE:  The entries in this table MUST be in the same order as the  */
/*        enumerated type GLP_PRES_DEV.                               */

const U_INT16 GLP_PRES_MSG_TBL[][3] =
{
  { GLP_F_INLET_PSI_MSG, GLP_F_INLET_BAR_MSG, GLP_F_INLET_KPA_MSG },
  { GLP_B_INLET_PSI_MSG, GLP_B_INLET_BAR_MSG, GLP_B_INLET_KPA_MSG },
  { GLP_AUX_3_PSI_MSG, GLP_AUX_3_BAR_MSG, GLP_AUX_3_KPA_MSG },
  { GLP_AUX_4_PSI_MSG, GLP_AUX_4_BAR_MSG, GLP_AUX_4_KPA_MSG },
  { GLP_AUX_5_PSI_MSG, GLP_AUX_5_BAR_MSG, GLP_AUX_5_KPA_MSG },
  { GLP_PSI_SETPT_MSG, GLP_BAR_SETPT_MSG, GLP_KPA_SETPT_MSG }
};

U_INT16 GetGlpPresMsg( U_INT16 dev )
{
   return( GLP_PRES_MSG_TBL[dev][GetPresUnits()] );
}



/***********************************************************************/
/* FUNCTION:  DeviationPresent                                         */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:  To return a TRUE for a present deviation entry and a      */
/*           FALSE for an entry number greater than the number of      */
/*           deviations logged.                                        */
/*                                                                     */
/* RETURNED VALUE:  BIT8 boolean                                       */
/*                                                                     */
/* GLOBALS USED:  Glp_Status                                           */
/*                                                                     */
/* CALLS:  none                                                        */
/*                                                                     */
/* LIMITATIONS:  Assumes that the calling routine begins requesting    */
/*               entries from index 0.                                 */
/*                                                                     */
/***********************************************************************/

BIT8 DeviationPresent( U_INT16 deviation )
{
   if ( DEVIATIONS_FOUND )
   {
      if ( Glp_Status.log_index > deviation )
      {
         return( TRUE );   /* there is a deviation entry */
      }
   }

   return( FALSE );        /* no deviation entry for this index */

}


/***********************************************************************/
/* FUNCTION:  TransferDeviationLine                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:  To transfer a 20-character line to a string for display-  */
/*           ing the glp run deviation log.                            */
/*                                                                     */
/* RETURNED VALUE: none                                                */
/*                                                                     */
/* GLOBALS USED:  Glp_Log                                              */
/*                                                                     */
/* CALLS:  TransferMsg                                                 */
/*                                                                     */
/* LIMITATIONS:  Assumes that the calling routine can handle the       */
/*               internation character set.                            */
/*                                                                     */
/***********************************************************************/

void TransferDeviationLine( U_INT16 log_index, U_INT16 line_no, BIT8 *string )
{
   /* Return the appropriate message based upon line number */

   switch( line_no )
   {
      case 1:
              TransferMsg ( Glp_Log[ log_index ].header_msg_no, string,
                            Glp_Log[ log_index ].value_2 );
         break;

      case 2:
              TransferMsgLeftJustify ( Glp_Log[ log_index ].msg_no, string,
                                       Glp_Log[ log_index ].value,
                                       Glp_Log[ log_index ].value_2 );
         break;

      case 3:
              TransferMsg ( AT_RUNTIME_MSG, string,
                            Glp_Log[ log_index ].run_time );
         break;
   }
}


/***********************************************************************/
/* FUNCTION:  GetGlpEntryLine                                          */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:  To return a line from the GLP log.  There are 3 lines     */
/*           per log.                                                  */
/*                                                                     */
/* RETURNED VALUE:  INT32 deviation code                               */
/*                                                                     */
/* GLOBALS USED:  Glp_Log                                              */
/*                                                                     */
/* CALLS:  TransferDeviationLine                                       */
/*                                                                     */
/* LIMITATIONS:  Does not reject invalid line_no's.  Expects calling   */
/*               function to test for out of range line_no.            */
/*                                                                     */
/***********************************************************************/

INT32 GetGlpEntryLine( U_INT16 log_index, U_INT16 line_no, BIT8 *string )
{
   /* This function expects the input index to be base 0 */
   /* so a 5 entry log would have indices from 0 to 4.   */
   /* It expects the line_no to be 1-3.                  */

   if ( GetMaxDeviation() <= log_index )  return -1;
/* if ( log_index         <  0 )          return -1; */

   TransferDeviationLine( log_index, line_no, string );

   return ( INT32 )Glp_Log[ log_index ].dev_code;
}

